package com.lc.user.express.ordermanagement;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lc.user.express.BaseFragment;
import com.lc.user.express.R;
import com.lc.user.express.adapter.OrderAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetOrderDelete;
import com.lc.user.express.httpserver.GetOrderList;
import com.lc.user.express.model.OrderModel;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.view.dialog.AffirmDialog;
import com.lc.user.express.view.pull.PullToRefreshBase;
import com.lc.user.express.view.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/2/26. 订单管理
 */
public class OrderManagementFragment extends BaseFragment {
    public static OnRefreshOrderData onRefreshOrderData = null;
    private PullToRefreshListView pListView;
    private OrderAdapter adapter;
    private List<OrderModel> list = new ArrayList<>();
    private int ispage;
    private int nextpage;

    private GetOrderList getOrderList = new GetOrderList(AppContext.myPreferences.getUid(), 1, new AsyCallBack<GetOrderList.Info>() {
        @Override

        public void onSuccess(String toast, int type, GetOrderList.Info info) throws Exception {
            ispage = info.ispage;
            nextpage = info.nextpage;
            if (type == 0) {
                list.clear();
            }
            list.addAll(info.dataList);
            adapter.updateListView(list);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);
            adapter.updateListView(list);
            pListView.onPullDownRefreshComplete();
            pListView.onPullUpRefreshComplete();
        }
    });

    @Override
    public View getContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_order_management, container, false);
        setTitle("订单管理");
        initView(v);

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        getOrderData(1, true, 0);
    }

    private void initView(View v) {
        pListView = (PullToRefreshListView) v.findViewById(R.id.lv);
        pListView.setPullLoadEnabled(false);
        pListView.setScrollLoadEnabled(true);
        pListView.setPullRefreshEnabled(true);
        pListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getOrderData(1, false, 0);
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (ispage == 1) {
                    getOrderData(nextpage, false, 1);
                } else {
                    try {
                        pListView.onPullDownRefreshComplete();
                        pListView.onPullUpRefreshComplete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        final ListView listView = pListView.getRefreshableView();
        listView.setDivider(null);
        listView.setDividerHeight(0);
        listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        adapter = new OrderAdapter(activity, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("orderid", list.get(position).getoId());

                if (list.get(position).getoStatus() == OrderModel.STATUS_SENDING_ORDER) {
                    if (list.get(position).getAddrlist().size() > 0) {
                        bundle.putString("spoint", list.get(position).getAddrlist().get(0).getPoint());
                    }
                    show(OrderSendingDetailsActivity.class, bundle);
                } else {
                    //list.get(position).getoStatus();
                    bundle.putString("orderstatus", list.get(position).getoStatus()+"");
                    Log.d(list.get(position).getoStatus()+"","mmmmmmmmmmmmmmmmmmmmmmmmmmmm");

                    show(OrderDetailsActivity.class, bundle);
                }

            }

        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                if (list.get(position).getoStatus() == OrderModel.STATUS_CANCEL) {
                new AffirmDialog(activity, "您确定要删除该订单吗？", "取消", "确定", R.style.MyDialog) {

                    @Override
                    protected void onYesClick() {

                            deleteOrder(list.get(position).getoId());
                        }


                }.show();
                }
                return true;
            }
        });


        onRefreshOrderData = new OnRefreshOrderData() {
            @Override
            public void onRefresh() {
                getOrderData(1, false, 0);
            }
        };
    }

    private void getOrderData(int page, boolean isShow, int type) {
        getOrderList.userid = AppContext.myPreferences.getUid();
        getOrderList.page = page;
        getOrderList.execute(activity, isShow, type);
    }

    public interface OnRefreshOrderData {
        void onRefresh();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        onRefreshOrderData = null;
    }
    private void deleteOrder(String orderid) {
        new GetOrderDelete(AppContext.myPreferences.getUid(), orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);
                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();
                }
            }
            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(activity, toast);
            }
        }).execute(activity);//调接口
    }
}
